<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CabinetController
 *
 * @author Admin
 */
class Planning_CabinetController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }

        $view = new Zend_View();
        $view->headTitle(' > Planning > Cabinet');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $cabinetModel = new Application_Model_CabinetModel();
        $paginator = $cabinetModel->getAllCabinets($this->_request);
        $this->view->paginator = $paginator;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $cabinetModel = new Application_Model_CabinetModel();
        $paginator = $cabinetModel->getAllCabinets($this->_request);
        $this->view->paginator = $paginator;
    }

    public function addAction() {
        $this->_helper->layout->disableLayout();
        $cabinetModel = new Application_Model_CabinetModel();
        //create form
        $form = new Application_Form_cabinet_AddForm();

        // if request is post
        if ($this->_request->isPost()) {

            // if form is valid

            if ($form->isValid($this->_request->getPost())) {

                $data = array(
                    'name' => $this->_request->getPost('cabinetName'),
                    'status' => $this->_request->getPost('status')
                );
                $cabinetModel->save($data);

             //   $this->_redirect("planning/cabinet");
              //  exit;
            }
        }

        // assign form variable to view
        $this->view->form = $form;
    }

    public function editAction() {
        $this->_helper->layout->disableLayout();
        $cabinetModel = new Application_Model_CabinetModel();

        $cabinetId = $this->_request->getParam('id');
        $cabinetDetail = $cabinetModel->getCabinet('id', $cabinetId);
        //create form
        $form = new Application_Form_cabinet_AddForm();

        $form->populate(array(
            'cabinetName' => $cabinetDetail['name'],
            'status' => $cabinetDetail['status']
        ));
        // if request is post
        if ($this->_request->isPost()) {

            // if form is valid

            if ($form->isValid($this->_request->getPost())) {

                $data = array(
                    'id' => $cabinetId,
                    'name' => $this->_request->getPost('cabinetName'),
                    'status' => $this->_request->getPost('status')
                );
                $cabinetModel->save($data);

            //    $this->_redirect("planning/cabinet");
             //   exit;
            }
        }

        // assign form variable to view
        $this->view->form = $form;
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $cabinetId = $this->_request->getParam('id');
        $cabinetModel = new Application_Model_CabinetModel();

        $cabinetModel->delete("id = '$cabinetId'");
     //   $this->_redirect('planning/cabinet');
    }
    
    public function cabinetdetailAction(){
        $this->_helper->layout->disableLayout();
        
        $cabinetId = $this->_request->getParam('id');
        $documentModel = new Application_Model_DocumentModel();
        
        $files = $documentModel->getAllDocument('cabinet', $cabinetId);
        $this->view->files = $files;        
    }
}